package server

import (
	"fmt"
	"net/http"
	"strings"

	"git.pyer.club/kingecg/gologger"
)

type Directive func(args ...string) Middleware

var Add_Header Directive = func(args ...string) Middleware {
	return func(w http.ResponseWriter, r *http.Request, next func()) {
		l := gologger.GetLogger("Directive")
		p := args[1:]
		params := strings.Join(p, " ")
		l.Debug(fmt.Sprintf("Add-Header %s:%s", args[0], params))
		w.Header().Add(args[0], args[1])
		next()
	}
}

var Set_Header Directive = func(args ...string) Middleware {
	return func(w http.ResponseWriter, r *http.Request, next func()) {
		l := gologger.GetLogger("Directive")
		p := args[1:]
		params := strings.Join(p, " ")
		l.Debug(fmt.Sprintf("Set-Header %s:%s", args[0], params))
		w.Header().Set(args[0], params)
		next()
	}
}

var DirectiveMap = map[string]Directive{
	"Set-Header": Set_Header,
	"Add-Header": Add_Header,
}
